package com.leha.mp.api.controller;

import com.github.pagehelper.PageInfo;
import com.leha.mp.api.form.OrderForm;
import com.leha.mp.core.Utils.Page;
import com.leha.mp.core.entity.mybatis.PlConsulting;
import com.leha.mp.core.entity.vo.DoctorLoginVO;
import com.leha.mp.core.services.AppDoctorServices;
import com.leha.mp.core.services.ConfluenceServices;
import com.leha.mp.core.services.ErrorMessageServices;
import constants.APIErrorDict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import utils.APIResult;

import javax.ws.rs.FormParam;
import javax.ws.rs.Path;
import javax.ws.rs.QueryParam;

@RestController
@Api(value = "/doctor", description = "医生类API")
@RequestMapping("/doctor")
@ResponseStatus(HttpStatus.OK)
public class DoctorController {
    @Autowired
    AppDoctorServices doctorServices;
    @Autowired
    ConfluenceServices confluenceServices;
    @Autowired
    ErrorMessageServices errorMessageServices;

    @RequestMapping(value = "/{doctorId}", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(value="获取医生详细资料_用户端")
    APIResult saveCase(@PathVariable Integer doctorId, Integer userId) {
        return new APIResult(doctorServices.getDoctorRichInfo(doctorId, userId));
    }

    @PostMapping("/consulting")
    @ApiOperation(value="私人医生问询_用户端")
    APIResult openConsulting(OrderForm order) {
        PlConsulting res = doctorServices.genVipConsulting(order.getUserId(), order.getProductId(), order.getDoctorId(), order.getExtraData());
        if(res==null) {
            return new APIResult(errorMessageServices.getLastError());
        }
        return new APIResult(res);
    }

    @PostMapping("/phone/srv")
    @ApiOperation("医生开始电话服务，发起双呼_医生端")
    APIResult beginPhoneServ(Integer consultingId) {
        return new APIResult(confluenceServices.beginPhoneServ(consultingId));
    }

    @PostMapping("/finishConsulting")
    @ApiOperation("医生结束咨询_医生端")
    APIResult endConsulting(@FormParam(value="doctorId") Integer doctorId,
                            @FormParam(value="consultingId") Integer consultingId,
                            @FormParam(value="report") String report) {
        return new APIResult(confluenceServices.doctorFinishConsulting(doctorId, consultingId, report));
    }

    @PostMapping("/video/srv")
    @ApiOperation("医生开始视频服务_医生端")
    APIResult beginVideoServ(Integer consultingId) {
        return new APIResult(confluenceServices.beginVideoServ(consultingId));
    }

    @PostMapping("/video/endSrv")
    @ApiOperation("结束视频通话(退出视频房间)_医生端")
    APIResult endVideoServ(Integer consultingId, Integer doctorId, String roomId) {
        return new APIResult(confluenceServices.endVideo(doctorId, consultingId, roomId));
    }

    @PostMapping("evaluate")
    @ApiOperation("评价医生_用户端")
    APIResult evaluateDoctor(Integer consultingId, Integer star, String impress, String content, Boolean isAnonymous) {
        return new APIResult(confluenceServices.Evaluate(consultingId, star, impress, content, isAnonymous));
    }

    @GetMapping("label")
    @ApiOperation("获取评价标签_用户端")
    APIResult getEvaluateLabel() {
        return new APIResult(confluenceServices.getEvaluateLabel());
    }

    @GetMapping("srvTimes")
    @ApiOperation("获取医生服务时间_用户端")
    APIResult getDoctorServTime(Integer doctorId) {
        return new APIResult(doctorServices.getDoctorSrvTimes(doctorId));
    }

    @PostMapping("acceptConsulting")
    @ApiOperation("接受咨询_医生端")
    APIResult accetpConsulting(Integer doctorId, Integer consultingId){
        if(doctorServices.acceptConsulting(doctorId, consultingId)) {
            return new APIResult("OK");
        } else {
            return new APIResult(errorMessageServices.getLastError());
        }
    }

    @PostMapping("rejectConsulting")
    @ApiOperation("拒绝咨询_医生端")
    APIResult rejectConsulting(Integer doctorId, Integer consultingId){
        if(doctorServices.rejectConsulting(doctorId, consultingId)) {
            return new APIResult("OK");
        } else {
            return new APIResult(errorMessageServices.getLastError());
        }
    }

    @PostMapping("cancelSchedule")
    @ApiOperation("取消预约")
    APIResult cancelConsulting(Integer doctorId, Integer consultingId){
        if(doctorServices.cancelConsulting(doctorId, consultingId)) {
            return new APIResult("OK");
        } else {
            return new APIResult(errorMessageServices.getLastError());
        }
    }

    @GetMapping("getMySchedule")
    @ApiOperation("获取我的日常安排_医生端")
    APIResult getMySchedule(Integer doctorId, String beginTime, String endTime) {
        return new APIResult(doctorServices.getDoctorSchedule(doctorId, beginTime, endTime));
    }

    @RequestMapping(value = "/{uid}/update", method = {RequestMethod.POST})
    @ApiOperation(value = "上传医生资料_医生端", notes = "传输用户资料")
    APIResult login(@FormParam(value = "sessionKey") String sessionKey,
                    @FormParam(value = "encryptedData") String encryptedData,
                    @FormParam(value = "iv") String iv,
                    @PathVariable(value = "uid") Integer uid) {

        DoctorLoginVO loginUser = doctorServices.saveInfo(2, uid, sessionKey, encryptedData, iv);
        if (loginUser == null) {
            return new APIResult(errorMessageServices.getLastError());
        } else {
            return new APIResult(loginUser);
        }
    }

    @RequestMapping(value = "/enter", method = {RequestMethod.POST, RequestMethod.GET})
    @ApiOperation(value = "登录_医生端", notes = "直接传code，后台获取openid，匹配对应UID")
    APIResult loginV2(@QueryParam(value="code") String code) {
        if (code == null) {
            return new APIResult(APIErrorDict.PARAMERROR);
        }
        DoctorLoginVO loginUser = doctorServices.enter(2, code);
        if (loginUser == null) {
            return new APIResult(errorMessageServices.getLastError());
        } else {
            return new APIResult(loginUser);
        }
    }

    @GetMapping(value="{doctorId}/consulting")
    @ApiOperation(value="获取问诊_医生端")
    APIResult getMyConsulting(@PathVariable Integer doctorId,
                              @QueryParam(value="type") Integer type,
                              Page page) {
        return new APIResult(new PageInfo(doctorServices.getMyConsulting(doctorId, type, page)));
    }

    @GetMapping(value="{doctorId}/patients")
    @ApiOperation(value="患者列表_医生端")
    APIResult getMyPatients(@PathVariable Integer doctorId)
    {
        return new APIResult(doctorServices.getMyPatients(doctorId));
    }

    ////////服务类 开通关闭服务
    @PostMapping(value="/srvs/{doctorId}")
    @ApiOperation(value="开通关闭服务,type=1,2,3,4,5,6,7,op=0，1开通关闭_医生端")
    APIResult updateDoctorServes(@PathVariable Integer doctorId, @FormParam(value="type") Integer type,@FormParam(value="op") Integer op)
    {
        return new APIResult(doctorServices.updateDoctorServis(doctorId, type, op));
    }

    @GetMapping(value="/srvs/{doctorId}")
    @ApiOperation(value="获取医生开通的服务_医生端")
    APIResult getDoctorServices(@PathVariable Integer doctorId) {
        return new APIResult(doctorServices.getDoctorSercesStatus(doctorId));
    }

    @PostMapping(value="/srvTimes/{doctorId}")
    @ApiOperation(value="跟新服务时间，新的服务时间数组_医生端")
    APIResult updateDoctorServicesTimes(@PathVariable Integer doctorId, @FormParam(value="index") Integer index, @FormParam(value="values") String values) {
        return new APIResult(doctorServices.updateDoctorServisTimes(doctorId, index, values));
    }

    @GetMapping(value="/srvTimes/{doctorId}")
    @ApiOperation(value="获取医生服务时间_医生端")
    APIResult getDoctorServicesTimes(@PathVariable Integer doctorId) {
        return new APIResult(doctorServices.getDoctorSercesTimes(doctorId));
    }

    @GetMapping(value="/case/{doctorId}")
    @ApiOperation(value="获取我的患者请求")
    APIResult getMyCase(@PathVariable Integer doctorId) {
        return new APIResult(doctorServices.getNeedCheckedMyCase(doctorId));
    }

    @GetMapping(value="/vip/{doctorId}")
    @ApiOperation(value="获取我的私人医生请求, type=0所有的，type=1待确认的")
    APIResult getMyVips(@PathVariable Integer doctorId, Integer type) {
        return new APIResult(doctorServices.getMyVips(doctorId, type));
    }

    @PostMapping(value="/vip")
    @ApiOperation(value="接受私人医生请求, op=0通过，op=1拒绝")
    APIResult opVipRecord(Integer doctorId, Integer vipId, Integer op) {
        return new APIResult(doctorServices.acceptVipRequest(doctorId, vipId, op));
    }

    @PostMapping(value = "/case")
    @ApiOperation(value="接受患者请求, op=0通过，op=1拒绝")
    APIResult opCaseRecord(Integer doctorId, Integer caseId, Integer op) {
        return new APIResult(doctorServices.acceptCaseRequest(doctorId, caseId, op));
    }
}
